package com.jinyu.gateway.server;

import io.netty.channel.Channel;
import lombok.Data;

import java.util.concurrent.ConcurrentHashMap;

@Data
public class NettyServer {
    private int port;
    private String address;

    private static ConcurrentHashMap<Channel, Channel> channelMap = new ConcurrentHashMap<>();

    private final Channel channel;

    public NettyServer(Channel channel) {
        this.channel = channel;
    }

    public static void addChannel(Channel channel) {
        channelMap.put(channel, channel);
    }

    public static void removeChannel(Channel channel) {
        channelMap.remove(channel);
    }

    public static void sendMsg(Channel channel, String msg) {
        if (channelMap.containsKey(channel)) {
            channelMap.get(channel).writeAndFlush(msg);
        }
    }
}
